<template>
  <div class="app-container">

    <!--  学生管理 查询条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select  v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option v-for="item in studentSexOptian" :label="item.dictLabel" :value="item.dictValue" :key="item.dictCode"/>
        </el-select>
      </el-form-item>
<!--        <el-form-item label="性别" prop="delTime">-->
<!--            <el-date-picker clearable size="small" style="width: 200px"-->
<!--                            v-model="queryParams.delTime"-->
<!--                            type="datetime"-->
<!--                            value-format="yyyy-MM-dd HH:mm:ss"-->
<!--                            placeholder="选择性别">-->
<!--            </el-date-picker>-->
<!--        </el-form-item>-->
      <el-form-item label="学生状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择学生状态" clearable size="small">
          <el-option v-for="item in studentStatusOptian" :label="item.dictLabel" :value="item.dictValue" :key="item.dictCode" />
        </el-select>
      </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <!-- 表格上方 学生管理 功能键 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:student:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>


    <!-- 学生管理 表格 -->
    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

            <el-table-column label="主键" align="center" prop="id" v-if="false"/>
                    <el-table-column label="姓名" align="center" prop="name" v-if="columns[0].visible"/>
                        <el-table-column label="学号" align="center" prop="studentId" v-if="columns[1].visible"/>
                        <el-table-column  label="性别" align="center" prop="sex" v-if="columns[2].visible">
                          <template slot-scope="scope">
                            <span v-if="scope.row.sex===0">
                              女
                            </span>
                            <span v-else>
                              男
                            </span>
                          </template>
                        </el-table-column>
                                        <!-- <el-table-column label="性别" align="center" prop="delTime" width="180" v-if="columns[3].visible">
            <template slot-scope="scope">
                <span>{{ parseTime(scope.row.delTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
        </el-table-column> -->
                            <el-table-column label="学生状态" align="center" prop="status" v-if="columns[4].visible">
                              <template slot-scope="scope">
                                <span v-if="scope.row.status===0">
                                  正常
                                </span>
                                <span v-else>
                                  冻结
                                </span>
                              </template>
                            </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:student:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:student:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="changeStatus(scope.row)"
            v-hasPermi="['system:student:edit']"
          >修改状态</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="学号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option v-for="item in studentSexOptian" :label="item.dictLabel" :value="item.dictValue" :key="item.dictCode"/>
          </el-select>
        </el-form-item>
<!--    <el-form-item label="性别" prop="delTime">-->
<!--        <el-date-picker clearable size="small" style="width: 200px"-->
<!--                        v-model="form.delTime"-->
<!--                        type="datetime"-->
<!--                        value-format="yyyy-MM-dd HH:mm:ss"-->
<!--                        placeholder="选择性别">-->
<!--        </el-date-picker>-->
<!--    </el-form-item>-->
<!--        <el-form-item label="性别" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入性别" />-->
<!--        </el-form-item>-->
        <el-form-item label="学生状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态"  >
            <el-option v-for="item in studentStatusOptian" :label="item.dictLabel" :value="item.dictValue" :key="item.dictCode" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent, exportStudent,changeStatus
} from "@/api/system/student";
import { getToken } from "@/utils/auth";

import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数

export default {
  name: "Student",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生管理表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        studentId: undefined,
        sex: undefined,
        delTime: undefined,
        status: undefined
      },
      // 列信息
      columns: [
                        { key: 0, label: `姓名`, visible: true },
                    { key: 1, label: `学号`, visible: true },
                    { key: 2, label: `性别`, visible: true },
                                    { key: 3, label: `性别`, visible: true },
                        { key: 4, label: `学生状态`, visible: true },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      studentSexOptian:[],
      studentStatusOptian:[],
    }
  },
  created() {
    this.getList();
    this.getDicts('student_sex').then(res =>{
      this.studentSexOptian = res.data;
    }),
    this.getDicts('student_status').then(res =>{
      this.studentStatusOptian = res.data;
    })
  },
  methods: {
    checkPermi,
    checkRole,
    /** 查询学生管理列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      changeStatus(rows){
      let status = 1;
      if(rows.status === 1){
        status=0;
      }
      changeStatus({id:rows.id,status:status}).then(res =>{
        this.getList();
      })
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        studentId: undefined,
        sex: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        delTime: undefined,
        delFlag: undefined,
        status: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.form.sex = response.data.sex.toString();
        this.form.status = response.data.status.toString();
        this.open = true;
        this.title = "修改学生管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除学生管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学生管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStudent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
